<script type="text/javascript">

function update_menu_as_logged_in(_name)
{
	$('#btn_login').empty();
	$('#btn_create_account').empty();
	if($('#btn_logout').text() == ""){//if this is called again it doesn't append names if they're already appended
		$('#username_menu_display').append('Logged in as ' + _name);
		//$('#btn_view_custom_list').append('My Restaurants');
		$('#btn_add_restaurant').append('Add Restaurant');
		$('#btn_logout').append('Logout');
		$('#btn_sent_tips').append('Recommend');
		$('#btn_recommendations').append('Inbox');
	}
	
}

function check_login()
{
	$.ajax({
        url: "<?php echo site_url('account/check_session');?>",
		type: 'POST',
        dataType: 'json',
        
        success: function (msg) {
			if(msg.code=="offline"){
				//run update again
				setTimeout("check_login()", 1000);
			}else{
				//user is online
				update_menu_as_logged_in(msg.username);
				document.getElementById('css1').href = '<?php echo base_url();?>application/views/css/'+msg.style;
				switch_bg(msg.background);
				var _window = $('#submit_login').closest("div");
				close_window(_window);
			}
        }
      });
}

$(document).ready(function () {
	$('#username_login').select();

	$('#submit_login').click(function(){
		//$("#submit_display").empty();
		//$("#submit_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
		
		if($('#username_login').val()!="" && $('#password_login').val()!=""){
			var form_data = {
				name: $('#username_login').val(),
				pass: $('#password_login').val()
			};
			
			$.ajax({
	           url: "<?php echo site_url('account/check_login_data');?>",
			   type: 'POST',
	           data: form_data,
	           dataType: 'json',
	           
	           success: function (msg) {
					var _text = "error";
					var _color = "red";
					if(msg.message!="ok"){
						_text = "Incorrect Account, name and/or pass";
						_color = "orange";
					}else{
						_text = "Logging in...";
						_color = "green";
	
						//close the window
						var _window = $('#submit_login').closest("div");
						close_window(_window);

						update_menu_as_logged_in(form_data['name']);

						document.getElementById('css1').href = '<?php echo base_url();?>application/views/css/'+msg.style;
						switch_bg(msg.background);
					}
					$("#submit_login_display").empty();
					$("#submit_login_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
					
					
	           }
	         });
		}
		else{
			$("#submit_login_display").empty();
			$("#submit_login_display").append("<div style='color: orange;'>Some field(s) are empty</div>");
		}
		 
		return false;
	});

	$('#btn_google_login').click(function(){
		setTimeout("check_login()", 1000);
		window.open($('#btn_google_login').attr('name'),'mywindow','width=800,height=600,left=100,top=100,screenX=100,screenY=100')
		//$("#btn_google_login").empty();
		$("#btn_google_login").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
		
		 
		return false;
	});

});

</script>